package com.ucenter.server.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ucenter.server.auth.dao.RoleInfoDao;
import com.ucenter.server.auth.pojo.RoleInfo;
import com.ucenter.server.auth.service.RoleInfoService;
import com.ucenter.server.user.service.WebsiteService;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;
import com.system.handle.model.ResponseCode;

/**
 * role_info的Service
 * @author autoCode
 * @date 2018-12-01 09:50:44
 * @version V1.0.0
 */
@Component
public class RoleInfoServiceImpl implements RoleInfoService {

	@Autowired
	private RoleInfoDao roleInfoDao;
	@Autowired
	private WebsiteService websiteService;
	
	@Override
	public ResponseFrame saveOrUpdate(RoleInfo roleInfo) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(roleInfo.getRoleId())) {
			roleInfo.setRoleId(FrameNoUtil.uuid());
			roleInfoDao.save(roleInfo);
		} else {
			roleInfoDao.update(roleInfo);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public RoleInfo get(String roleId) {
		return roleInfoDao.get(roleId);
	}

	@Override
	public ResponseFrame pageQuery(RoleInfo roleInfo) {
		roleInfo.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = roleInfoDao.findRoleInfoCount(roleInfo);
		List<RoleInfo> data = null;
		if(total > 0) {
			data = roleInfoDao.findRoleInfo(roleInfo);
			for (RoleInfo role : data) {
				role.setWebsiteName(websiteService.getName(role.getWebsiteId()));
			}
		}
		Page<RoleInfo> page = new Page<RoleInfo>(roleInfo.getPage(), roleInfo.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String roleId) {
		ResponseFrame frame = new ResponseFrame();
		roleInfoDao.delete(roleId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
}